<?php

namespace App\Services\MiniprogramToGame\HotSearch;

use App\Services\ServiceBase;
use App\Structs\HotSearchStruct;
use Illuminate\Support\Facades\Http;

class Zhihu extends ServiceBase
{
    public function reptile()
    {
        $url = 'https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=50&desktop=true';
        $response = Http::get($url);
        if (!$response->ok()) {
            return false;
        }

        $data = json_decode($response->body(), true);

        $list = [];
        foreach ($data['data'] as $index => $item) {
            preg_match('/\d+/',  $item['detail_text'], $hot);
            $hot_value       = $hot[0] ?? 0 * 10000;
            $class           = new HotSearchStruct;
            $class->title    = $item['target']['title'];
            $class->desc     = $item['target']['excerpt'];
            $class->hotIndex = $index+1;
            $class->hotValue = $hot_value;
            $class->url      = "https://www.zhihu.com/question/".$item['target']['id'];
            $class->img      = $item['children'][0]['thumbnail'] ?? '';
            $list[] = $class;
        }

        return $list;
    }
}
